package id.web.faisalabdillah.xtimesheet.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.stereotype.Component;

import id.web.faisalabdillah.xtimesheet.dao.IFileStorageDao;
import id.web.faisalabdillah.xtimesheet.domain.FileStorage;

@Component
public class FileStorageDaoImpl implements IFileStorageDao {

	private JpaTemplate jpaTemplate;
	
	@Autowired
	protected void setJpaTemplate(EntityManagerFactory entityManagerFactory){
		this.jpaTemplate = new JpaTemplate(entityManagerFactory);
	}
	
	@Override
	public void insert(FileStorage storage) {
		jpaTemplate.persist(storage);
	}

	@Override
	public void update(FileStorage storage) {
		jpaTemplate.merge(storage);
	}

	@Override
	public void remove(FileStorage storage) {
		jpaTemplate.remove(storage);
	}

	@Override
	public FileStorage findByCodename(String codename) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("codename", codename);
		return (FileStorage) jpaTemplate.findByNamedParams("select f from FileStorage f where codename  = :codename", map).get(0);
	}

	@Override
	public FileStorage findById(long id) {
		return jpaTemplate.find(FileStorage.class, id);
	}

	@Override
	public FileStorage findByIdNFile(long id) {
		final long fid=id;
		Object[] arr=jpaTemplate.execute(new JpaCallback<Object[]>() {

			@Override
			public Object[] doInJpa(EntityManager em) throws PersistenceException {
				Query qry=em.createQuery("select f.id,f.contentType,f.description,f.file_name,f.codename from FileStorage f where f.id = ?1");
				qry.setParameter(1, fid);
				return (Object[]) qry.getSingleResult();
			}
		});
		return new FileStorage(Long.parseLong(arr[0].toString()), String.valueOf(arr[1]), String.valueOf(arr[2]), String.valueOf(arr[3]), null, String.valueOf(arr[4]));
	}

	@Override
	public List<FileStorage> listAll() {
		List listFile=jpaTemplate.find("select f.id,f.contentType,f.description,f.file_name,f.codename from FileStorage f");
		List<FileStorage> listFileStorages=new ArrayList<FileStorage>();
		for (Iterator iterator = listFile.iterator(); iterator.hasNext();) {
			Object[] arr = (Object[]) iterator.next();
			listFileStorages.add(new FileStorage(Long.parseLong(arr[0].toString()), String.valueOf(arr[1]), String.valueOf(arr[2]), String.valueOf(arr[3]), null, String.valueOf(arr[4])));
		}
		return listFileStorages;
	}

	@Override
	public List<FileStorage> listPaging(int first,int max) {
		final int f=first;
		final int m=max;
		List listFile=jpaTemplate.execute(new JpaCallback<List>() {
			
			@Override
			public List doInJpa(EntityManager em) throws PersistenceException {
				Query q=em.createQuery("select f.id,f.contentType,f.description,f.file_name,f.codename from FileStorage f");
				q.setFirstResult(f);
				q.setMaxResults(m);
				return q.getResultList();
			}
		});
		
		List<FileStorage> listFileStorages=new ArrayList<FileStorage>();
		for (Iterator iterator = listFile.iterator(); iterator.hasNext();) {
			Object[] arr = (Object[]) iterator.next();
			listFileStorages.add(new FileStorage(Long.parseLong(arr[0].toString()), String.valueOf(arr[1]), String.valueOf(arr[2]), String.valueOf(arr[3]), null, String.valueOf(arr[4])));
		}
		return listFileStorages;
	}

	@Override
	public int sizeAll() {
		return jpaTemplate.execute(new JpaCallback<Integer>() {

			@Override
			public Integer doInJpa(EntityManager em)
					throws PersistenceException {
				Query q =em.createQuery("Select count(1) from FileStorage");
				return (Integer) q.getSingleResult();
			}
		});
	}

}
